home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / lib / file-input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-30  |  3.1 KB  |  159 lines

  1. /* file-input.c: file reading routines for binary files in BigEndian
  2.    order, 2's complement representation.
  3.  
  4. Copyright (C) 1992 Free Software Foundation, Inc.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "config.h"
  21.  
  22. #include "file-input.h"
  23.  
  24.  
  25. one_byte
  26. get_byte (FILE *f, string filename)
  27. {
  28.   one_byte b;
  29.  
  30.   if (fread (&b, 1, 1, f) != 1)
  31.     FATAL_PERROR (concat3 ("get_byte (", filename, ")"));
  32.  
  33.   return b;
  34. }
  35.  
  36.  
  37. two_bytes
  38. get_two (FILE *f, string filename)
  39. {
  40.   two_bytes b;
  41.  
  42.   b = get_byte (f, filename) << 8;
  43.   b |= get_byte (f, filename);
  44.  
  45.   return b;
  46. }
  47.  
  48.  
  49. four_bytes
  50. get_four (FILE *f, string filename)
  51. {
  52.   four_bytes b;
  53.  
  54.   b = get_byte (f, filename) << 24;
  55.   b |= get_byte (f, filename) << 16;
  56.   b |= get_byte (f, filename) << 8;
  57.   b |= get_byte (f, filename);
  58.  
  59.   return b;
  60. }
  61.  
  62.  
  63. /* In order to get a signed value, we merely read an unsigned value and
  64.    cast it; the value in the file is assumed to be 2's complement.  */
  65.  
  66. signed_4_bytes
  67. get_signed_four (FILE *f, string filename)
  68. {
  69.   return (signed_4_bytes) get_four (f, filename);
  70. }
  71.  
  72.  
  73. /* Return the next N bytes in the file F as an array.  */
  74.  
  75. address
  76. get_n_bytes (unsigned n, FILE *f, string filename)
  77. {
  78.   one_byte *b;
  79.  
  80.   if (n == 0)
  81.     FATAL1 ("get_n_bytes (%s): can't get zero bytes", filename);
  82.  
  83.   b = xmalloc (n);
  84.  
  85.   if (fread (b, n, 1, f) != 1)
  86.     FATAL2 ("get_n_bytes (%s): fread of %u bytes failed", filename, n);
  87.  
  88.   return b;
  89. }
  90.  
  91.  
  92.  
  93. /* Reading backwards. This macro is shared among all the routines by assuming
  94.    the name `f' for the file pointer.  */
  95.  
  96. #define MOVE_BACK(size) xfseek (f, (long) -size, SEEK_CUR, filename)
  97.  
  98. one_byte
  99. get_previous_byte (FILE *f, string filename)
  100. {
  101.   one_byte b;
  102.  
  103.   MOVE_BACK (1);
  104.   b = get_byte (f, filename);
  105.   MOVE_BACK (1);
  106.   return b;
  107. }
  108.  
  109.  
  110. two_bytes
  111. get_previous_two (FILE *f, string filename)
  112. {
  113.   two_bytes b;
  114.  
  115.   MOVE_BACK (2);
  116.   b = get_two (f, filename);
  117.   MOVE_BACK (2);
  118.  
  119.   return b;
  120. }
  121.  
  122.  
  123.  
  124. four_bytes
  125. get_previous_four (FILE *f, string filename)
  126. {
  127.   four_bytes b;
  128.  
  129.   MOVE_BACK (4);
  130.   b = get_four (f, filename);
  131.   MOVE_BACK (4);
  132.  
  133.   return b;
  134. }
  135.  
  136.  
  137.  
  138. /* Looking for specific values in the input.  */
  139.  
  140. void 
  141. match_byte (one_byte expected, FILE *f, string filename)
  142. {
  143.   one_byte b = get_byte (f, filename);
  144.  
  145.   if (b != expected)
  146.     FATAL3 ("%s: Expected byte value %u, found %u", filename, expected, b); 
  147. }
  148.  
  149.  
  150. void
  151. match_previous_byte (one_byte expected, FILE *f, string filename)
  152. {
  153.   one_byte b = get_previous_byte (f, filename);
  154.  
  155.   if (b != expected)
  156.     FATAL3 ("%s: Expected previous byte value %u, found %u", filename, 
  157.             expected, b);
  158. }
  159.